package com.serene.authority.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serene.authority.admin.entity.MyDict;
import com.serene.authority.admin.mapper.DictMapper;
import com.serene.authority.admin.service.DictDetailService;
import com.serene.authority.admin.service.DictService;
import com.serene.authority.common.exceptionhandler.MyException;
import com.serene.authority.common.utils.Result;
import com.serene.authority.common.utils.ResultCode;
import com.serene.authority.common.utils.UserConstants;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.hutool.core.date.DateTime.now;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, MyDict> implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private DictDetailService dictDetailService;


    /**
     * 分页返回字典
     *
     * @param offectPosition
     * @param limit
     * @param myDict
     * @return
     */
    @Override
    public Result<MyDict> getDictPage(Integer offectPosition, Integer limit, MyDict myDict) {
        Page page = PageHelper.offsetPage(offectPosition, limit);
        List<MyDict> fuzzyDictByPage = dictMapper.getFuzzyDictByPage(myDict);
        return Result.ok().count(page.getTotal()).data(fuzzyDictByPage).code(ResultCode.TABLE_SUCCESS);
    }

    /**
     * 模糊查询数据字典
     *
     * @param myDict
     * @return
     */
    @Override
    public List<MyDict> queryDictLikeList(MyDict myDict) {
        QueryWrapper<MyDict> wrapper = new QueryWrapper<>();
        wrapper.like("dict_name", myDict.getDictName());
        return dictMapper.selectList(wrapper);
    }

    @Override
    public MyDict getDictByName(String dictName) {
        QueryWrapper<MyDict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_name", dictName);
        return dictMapper.selectOne(wrapper);
    }

    @Override
    public String checkDictNameUnique(MyDict myDict) {
        MyDict info = getDictByName(myDict.getDictName());
        if (ObjectUtil.isNotEmpty(info) && !info.getDictId().equals(myDict.getDictId())) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertDict(MyDict myDict) {
        myDict.setCreateTime(now());
        myDict.setUpdateTime(now());
        return dictMapper.insert(myDict);
    }

    @Override
    public MyDict getDictById(Integer dictId) {
        QueryWrapper<MyDict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_id", dictId);
        return dictMapper.selectOne(wrapper);
    }

    @Override
    public int updateDict(MyDict myDict) {
        return dictMapper.updateDict(myDict);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteDictByIds(String ids) throws MyException {
        Integer[] dictIds = Convert.toIntArray(ids);
        for (Integer dictId : dictIds) {
            dictDetailService.deleteDictDetailById(dictId);
        }
        return dictMapper.deleteDictByIds(dictIds);
    }
}
